package com.yuanduan.mplus.presenter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.widget.TextView;

import com.cj.framework.app.BaseApplication;
import com.cj.framework.util.IOkHttp;
import com.cj.framework.util.Msg;
import com.cj.framework.util.OkHttpImp;
import com.cj.framework.util.RSAUtils;
import com.cj.framework.widget.CircleView;
import com.yuanduan.mplus.R;
import com.yuanduan.mplus.config.Config;
import com.yuanduan.mplus.db.dao.UserDao;
import com.yuanduan.mplus.db.entity.User;
import com.yuanduan.mplus.model.entity.RSUserResults;
import com.yuanduan.mplus.util.DateManager;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2016/9/8.
 */
public class Main_DataManager {
    private User user;
    private IOkHttp okHttp=new OkHttpImp();
    private Context context;
    private CircleView circleView;
    private TextView tv_zhou;
    private TextView tv_1;
    private TextView tv_2;
    private TextView tv_3;
    private TextView tv_4;
    private TextView tv_5;
    private TextView tv_6;
    private String[] texts=new String[6];
    private int safePoint;
    private int economicalPoint;
    private IOkHttp.IHttpCallback callback=new IOkHttp.IHttpCallback() {
        @Override
        public void onResponse(Msg msg, int index) {
            String message=msg.content();
            switch (index){
                case 0:
                    RSUserResults results= (RSUserResults) Config.decry(message,RSUserResults.class);
                    if(results.getRS()==1){
                        dealwithData(results);
                    }
                    break;
            }
        }

        @Override
        public void onError(Exception e) {

        }
    };

    private void dealwithData(RSUserResults results) {
        run(results.getSecurity(),results.getEconomic(),new String[]{results.getMileage(),results.getDrivingTime(),results.getMaxSpeed()+"",results.getLinear()+"",results.getRoute()+"",results.getWarning()+""});
    }

    public Main_DataManager(Context context){
        this.context=context;
        init();
    }

    private void init(){
        UserDao userDao= BaseApplication.getDaoSession().getUserDao();
        List list=userDao.queryBuilder().list();
        user= (User) list.get(0);
        circleView= (CircleView) ((Activity)context).findViewById(R.id.circleview);
        tv_1= (TextView) ((Activity)context).findViewById(R.id.cv_next_tv1);
        tv_2= (TextView) ((Activity)context).findViewById(R.id.cv_next_tv2);
        tv_3= (TextView) ((Activity)context).findViewById(R.id.cv_next_tv3);
        tv_4= (TextView) ((Activity)context).findViewById(R.id.cv_next_tv4);
        tv_5= (TextView) ((Activity)context).findViewById(R.id.cv_next_tv5);
        tv_6= (TextView) ((Activity)context).findViewById(R.id.cv_next_tv6);
        tv_zhou= (TextView) ((Activity)context).findViewById(R.id.main1_zhou);
        tv_zhou.setText("第"+DateManager.getDatInfos()[0][1]+"周");
        sendHttpMessage();
    }

    private void sendHttpMessage() {
        HashMap<String,String> hashMap=new HashMap<>();
        hashMap.put("UserPhone",user.getUserPhone());
        hashMap.put("UserPassword",user.getUserPwd());
        hashMap.put("TargetUserPhone",user.getUserPhone());
        hashMap.put("YearCode", DateManager.getDatInfos()[0][0]+"");
        hashMap.put("WeekCode",DateManager.getDatInfos()[0][1]+"");
        okHttp.httpInfoPost(Config.URL+Config.USERRESULTS,Config.translateMap(hashMap),callback,0);
    }


    public void run(int start,int end,String[] texts){
        circleView.start(start,end);
        tv_1.setText(texts[0]);
        tv_2.setText(texts[1]);
        tv_3.setText(texts[2]);
        tv_4.setText(texts[3]);
        tv_5.setText(texts[4]);
        tv_6.setText(texts[5]);
    }

    public void destroy(){
        context=null;
    }
}
